package treeStudy;

public class PathSum3 {

    /**
     * LeetCode T437 路径总和
     * @param args
     */
    public static void main(String[] args) {

    }

    public int pathSum(TreeNode root, int targetSum) {
        return root != null ? pathSumStartWithRoot(root, targetSum) + pathSum(root.left, targetSum) + pathSum(root.right, targetSum) : 0;
    }

    //进行求结果
    public int pathSumStartWithRoot(TreeNode root, int targetSum){
        if (root == null){
            return 0;
        }
        int count = targetSum == root.val ? 1 : 0;
        count += pathSumStartWithRoot(root.left, targetSum - root.val);
        count += pathSumStartWithRoot(root.right, targetSum - root.val);
        return count;
    }
}
